﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using Castle.ActiveRecord;
using SinsenWeb.Helpers;
using SinsenWeb.Models;

namespace SinsenWeb.Models
{
    [ActiveRecord]
    public class Member : EntityBase<Member>, IEntity
    {
        #region Actual ActiveRecord Props
        [PrimaryKey]
        public virtual int Id { get; set; }

        [Property]
        public virtual string Username { get; set; }

        [Property]
        public virtual string HashedPassword { get; set; }

        [Property]
        public virtual string Firstname { get; set; }

        [Property]
        public virtual string Lastname { get; set; }

        [Property]
        public virtual string Email { get; set; }

        [Property]
        public virtual string Address { get; set; }

        [Property]
        public virtual string ZipCode { get; set; }

        [Property]
        public virtual string City { get; set; }

        [Property]
        public virtual string Phone { get; set; }

        [Property]
        public virtual string MobilePhone { get; set; }

        [Property]
        public virtual DateTime? BirthDate { get; set; }

        [Property]
        public virtual DateTime? StartedDate { get; set; }

        [Property]
        public virtual string MsnAddress { get; set; }

        [Property]
        public virtual string AimAddress { get; set; }

        [Property]
        public virtual string SkypeAddress { get; set; }

        [Property]
        public virtual string PictureUrl { get; set; }

        [BelongsTo("InstrumentId")]
        public virtual Instrument Instrument { get; set; }

        /// <summary>
        /// 0 = Totally inactive, cannot log in
        /// 1 = Show as inactive, not included in "all active", but can log in
        /// 2 = Show as active, included in "all active", can log in
        /// </summary>
        [Property]
        public int ActiveState { get; set; }

        /// <summary>
        /// 0 = not insured
        /// 1 = insured without instrument
        /// 2 = insured with instrument
        /// </summary>
        [Property]
        public int InsuranceState { get; set; }

        /// <summary>
        /// (bitmapped rights)
        /// 0 = no rights (ordinary member)
        /// 1 = can edit articles
        /// 2 = can edit member attendance
        /// 255 = can admin
        /// </summary>
        [Property]
        public int SecurityRole { get; set; }


        [Property]
        public string NmfNumber { get; set; }


        [HasAndBelongsToMany(typeof(Role), Table = "MemberRole",
           ColumnKey = "MemberId", ColumnRef = "RoleId", 
           Lazy = false,Cascade = ManyRelationCascadeEnum.None)]
        public IList<Role> Roles
        {
            get; set;
        }

        #endregion


        public string FullName
        {
            get
            {
                if (string.IsNullOrEmpty(Firstname)
                    && string.IsNullOrEmpty(Lastname))
                {
                    return "anonymt medlem...";
                }
                return string.Empty + Firstname + " " + Lastname;
            }
        }

        public string InstrumentName
        {
            get
            {
                if (Instrument == null)
                    return "(ukjent)";
                return Instrument.Name;
            }
        }

        /// <summary>
        /// Temporary field used for accepting a new  password. Not actually stored. 
        /// (password is hashed before it is stored into HashedPassword property)
        /// </summary>
        public string Password { get; set; }

        public int InstrumentId
        {
            get
            {
                if (Instrument == null)
                {
                    return 0;
                }
                return Instrument.Id;
            }
        }


        public void GeneratePasswordHash()
        {
            HashedPassword = CryptoHelper.GetHash(Password);
        }

        public static Member FindByUsername(string username)
        {
            return FindAllByProperty("Username", username).FirstOrDefault();
        }

        public bool HasRole(SecurityRole role)
        {
            if((SecurityRole & role.GetHashCode()) == role.GetHashCode())
            {
                return true;
            }
            return false;
        }

        public string GetString_ActiveState()
        {
            switch (ActiveState)
            {
                case 2:
                    return "Inaktiv";
                default:
                    return "Aktiv";
            }
        }

        public static List<Member> FindAllActive()
        {
            return FindAllByProperty("ActiveState", 2).ToList();

        }
    }
}
